﻿<?php
	//ini_set("max_execution_time", 1);
	//header("Cache-Control: no-cache, must-revalidate");
	error_reporting(1);
include_once('config.php');

function openconnection() {
	$key = $GLOBALS['key'];
	$usrname = $GLOBALS['usrname'];
	$tns = $GLOBALS['tns'];
	$charset = $GLOBALS['charset'];
	$filename = 'serenco.txt';
	//echo "Test by Serenco";
	$password = decrypt_pass($key,$filename);
	$conn = oci_connect($usrname,$password,$tns,$charset) or die ('Khong ket noi duoc CSDL.');
	return $conn;
}

function encrypt_pass($key,$password,$filename){
	if(!$a = fopen($filename,'wb')){
		echo 'Can\'t open file';
		exit;
	}
	$en_password = rc4crypt($key,$password);
	fwrite($a,$en_password);
	fclose($a);
	return '1';
}

function decrypt_pass($key,$filename){
	if(!$a = fopen($filename,'rb')){
		echo 'Can\'t open file';
		exit;
	}
	$en_password = fread($a,filesize($filename));
	fclose($a);
	$password = rc4crypt($key,$en_password);
	return $password;
}


function getNumber($num){
	if(!empty($num)){
		$value = split_string($num,',');
		$truevalue = "";
		foreach($value as $tmp){
			$truevalue = $truevalue . $tmp;
		}
		return $truevalue;
	}
}

function redirect($filename) {
		if (!headers_sent()){
			header('Location: '.$filename);
		}else {
			echo '<script type="text/javascript">';
			echo 'window.location.href="'.$filename.'";';
			echo '</script>';
			echo '<noscript>';
			echo '<meta http-equiv="refresh" content="0;url='.$filename.'" />';
			echo '</noscript>';
		}
	}

function get_current_datestamp($strok){
    //get the current timestamp
    $currdate = gmdate("Ymd");
    $currday = substr($currdate,6,2);
    $currmonth = substr($currdate,4,2);
    $curryear = substr($currdate,0,4);
    $currdate_stamp = ($currday . $strok . $currmonth . $strok . $curryear);

    return $currdate_stamp;

}

function split_string($str,$token){
	$string = $str;
	$tok = strtok($string, $token );
	$i=0;
	$result="";
	while ($tok !== false) {
		$result[$i] = $result[$i] . $tok;
		$tok = strtok($token);
		$i++;
	}
	return $result;
}

function rc4crypt($pwd, $data)
{
    $key = array();
    $box = array();
    $pwdlen = strlen($pwd);
    $datalen = strlen($data);
    $result = '';

    for ($i = 0; $i < 256; $i++)
    {
        $key[$i] = ord($pwd[$i % $pwdlen]);
        $box[$i] = $i;
    }
    for ($j = $i = 0; $i < 256; $i++)
    {
        $j = ($j + $box[$i] + $key[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $datalen; $i++)
    {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $k = $box[($box[$a] + $box[$j]) % 256];
        $result .= chr(ord($data[$i]) ^ $k);
    }

    return $result;
}

/*Procedure */
function get_procedure_1($name){
	$conn = openconnection();
	$query = "begin $name(:v_ocur); end;"; //ham getcurrency khong truyen tham so p_key vo
	
    $v_ocur = oci_new_cursor($conn);
	$stmt =oci_parse($conn,$query) or die ('Can not parse query');
 	
    oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  
  
    oci_execute($stmt);
  
    oci_execute($v_ocur);

    oci_close($conn);
	 
	return $v_ocur;
}


//Ham nay dung khi ko can record tra ve. THuong la them moi roi sua 
function get_procedure_2($name,$p_msg){
	$conn = openconnection();
	$query = "begin $name(:p_msg,:v_ocur); end;";
	$v_ocur = oci_new_cursor($conn);
	$stmt =oci_parse($conn,$query) or die ('Can not parse query');

	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+20);
	
    oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  

    oci_execute($stmt);
  	oci_close($conn);
	return $p_msg;

}

//Ham nay dung khi ko can record tra ve. THuong la them moi roi sua 
function get_procedure_6($name,$p_msg){
	$conn = openconnection();
	$query = "begin $name(:p_msg); end;";
	$v_ocur = oci_new_cursor($conn);
	$stmt =oci_parse($conn,$query) or die ('Can not parse query');

	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+20);

    oci_execute($stmt);
  	oci_close($conn);
	return $p_msg;

}
// luu xuong csdl voi 2 tham so
function get_procedure_4($name,$p_msg,$p_msg1){
	$conn = openconnection();
	$query = "begin $name(:p_msg,:p_msg1,:v_ocur); end;";
	//echo $p_msg;
	$v_ocur = oci_new_cursor($conn);
	$stmt =oci_parse($conn,$query) or die ('Can not parse query');

	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+20);
	oci_bind_by_name($stmt,":p_msg1",$p_msg1,strlen($p_msg1)+20);
	
    oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  

    oci_execute($stmt);
  	oci_close($conn);
	//echo "<br>" . $p_msg;
	return $p_msg;

}

//Ham nay dung khi van tin select.
function get_procedure_3($name,$p_msg){
	
	$conn = openconnection();
	$query = "begin $name(:p_msg,:v_ocur); end;";
	$v_ocur = oci_new_cursor($conn);
	$stmt = oci_parse($conn,$query) or die ('Can not parse query');
	//echo $p_msg . "<br>";
	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+50);
	
    oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  
  
    oci_execute($stmt);
	oci_execute($v_ocur);
  	
    oci_close($conn);
	//echo $p_msg . "<br>";
	return $v_ocur;
	
}
//Ham nay dung khi van tin select voi 2 tham so.
function get_procedure_5($name,$p_msg,$p_msg1){
	$conn = openconnection();
	$query = "begin $name(:p_msg,:p_msg1,:v_ocur); end;";
	$v_ocur = oci_new_cursor($conn);
	$stmt = oci_parse($conn,$query) or die ('Can not parse query');
	//echo $p_msg . "<br>";
	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+50);
	oci_bind_by_name($stmt,":p_msg1",$p_msg1,strlen($p_msg)+50);
	
    oci_bind_by_name($stmt,":v_ocur",$v_ocur,-1,OCI_B_CURSOR);  
  
    oci_execute($stmt);
	oci_execute($v_ocur);
  	
    oci_close($conn);
	//echo $p_msg . "<br>";
	return $v_ocur;
	
}
/* function */
function get_function($name, $p_msg){
	$conn = openconnection();
	$stmt =oci_parse($conn,"begin :v_return:=$name(:p_msg); end;");    
	oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg));

    oci_bind_by_name($stmt,":v_return",$v_return,20);  //20: chiều dài của chuỗi lấy về.
   
    oci_execute($stmt);
 	oci_close($conn);

	return $v_return;
}

function date_format_2($string) {
	$str_format;
	if ($string == "0000-00-00" || $string == "0000-00-00 00:00:00") {
  		$string = date("d-m-Y");
	}
	$arrays = explode("-",$string);
	switch ($arrays[1]){
		case 'JAN': $arrays[1] = '01'; break;
		case 'FEB': $arrays[1] = '02'; break;
		case 'MAR': $arrays[1] = '03'; break;
		case 'APR': $arrays[1] = '04'; break;
		case 'MAY': $arrays[1] = '05'; break;
		case 'JUN': $arrays[1] = '06'; break;
		case 'JUL': $arrays[1] = '07'; break;
		case 'AUG': $arrays[1] = '08'; break;
		case 'SEP': $arrays[1] = '09'; break;
		case 'OCT': $arrays[1] = '10'; break;
		case 'NOV': $arrays[1] = '11'; break;
		case 'DEC': $arrays[1] = '12'; break;
	}
	$str_format = $arrays[2].$arrays[1].$arrays[0];
	return $str_format;
}

function check_hour($str){
	if(strlen($str)==5){
		$str_array=str_split($str);
		if($str_array[2]==':'){
			$value = explode(':',$str);
			if($value[0]>=0&&$value[0]<=23){
				if($value[1]>=0&&$value[1]<=59){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}else{
		return false;
	}
}
	
function check_date($str){
	if(strlen($str)==5){
		$str_array=str_split($str);
		if($str_array[2]=='/'){
			$value = explode('/',$str);
			if($value[1]>=1&&$value[1]<=12){
				if($value[1]==1||$value[1]==3||$value[1]==5||$value[1]==7||$value[1]==8||$value[1]==10||$value[1]==12){
					if($value[0]>=0&&$value[0]<=31){
						return true;
					}else{
						return false;
					}
				}elseif($value[1]==2){
					if($value[0]>=0&&$value[0]<=29){
						return true;
					}else{
						return false;
					}
				}else{
					if($value[0]>=0&&$value[0]<=30){
						return true;
					}else{
						return false;
					}
				}
			}else{
				return false;
			}
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function datetostring($date){
	$result = '';
	$arr = split('-',$date);
	$result = $result.$arr[2].$arr[1].$arr[0];
	return $result;
}

function datetostring_strok($date,$strok){
	$result = '';
	$arr = split($strok,$date);
	$result = $result.$arr[2].$arr[1].$arr[0];
	return $result;
}

function stringtodate($string){
	$result='';
	$result = strftime("%d-%m-%Y",strtotime($string));
	return $result;
}
?>